/**
  *@File文件:ImpCommissionManageService.java
  *@Date日期:2017年7月17日 下午8:27:48
  *@Copyright版权:Corporation 2017
  */
package com.qiaomengapp.serviceImp;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qiaomengapp.bean.Card;
import com.qiaomengapp.bean.CommissionDetail;
import com.qiaomengapp.bean.ShopWarp;
import com.qiaomengapp.bean.UserRecord;
import com.qiaomengapp.dao.CardMapper;
import com.qiaomengapp.dao.CommissionDetailMapper;
import com.qiaomengapp.dao.ShopMapper;
import com.qiaomengapp.dao.UserRecordMapper;
import com.qiaomengapp.exception.CustomException;
import com.qiaomengapp.service.ICommissionManageService;

/**
 * @Description描述:
 * @Author作者 wangzongwen
 * @Date日期:2017年7月17日
 */
@Service
public class ImpCommissionManageService implements ICommissionManageService {

	// 操作商铺表
	@Autowired
	private ShopMapper shopMapper;

	// 操作银行卡表
	@Autowired
	private CardMapper cardMapper;

	// 操作佣金明细表
	@Autowired
	private CommissionDetailMapper commissionDetailMapper;

	// 操作用户资金变动记录表
	@Autowired
	private UserRecordMapper userRecordMapper;

	@Override
	public void updateGetCommission(int shopId, double money, int userId, long cardCode) throws CustomException {

		// 根据商铺id查询商铺可用佣金
		double able_commission1 = shopMapper.select_able_commission(shopId);
		if (able_commission1 < money) {
			// 可用佣金小于提现金额不能进行提现
			throw new CustomException("可用佣金小于提现金额！");
		}

		// 当可用佣金大于等于100 提现金额小于100
		if (money < 100 && able_commission1 >= 100) {
			throw new CustomException("提现金额不能小于100！");
		}
		// 当可用佣金小于100 提现佣金不等于可用佣金
		if (money < 100 && able_commission1 != money) {
			throw new CustomException("可提现金额小于100，提现金额不等于可提现金额！");
		}

		// 减少商铺可用佣金
		int i = shopMapper.update_able_reduceCommission(money, shopId);
		// 添加一条商铺佣金记录
		CommissionDetail commissionDetail = new CommissionDetail();
		commissionDetail.setType(2);
		commissionDetail.setMoney(-money);
		commissionDetail.setCreatetime(new Date());
		commissionDetail.setShopId(shopId);
		// 根据商铺id得到商铺可用佣金
		double able_commission2 = shopMapper.select_able_commission(shopId);
		commissionDetail.setBalance(able_commission2);
		int j = commissionDetailMapper.insertCommissionDetail(commissionDetail);

		// 增加用户银行卡余额
		Card card = new Card();
		card.setCardCode(cardCode);
		int k = cardMapper.update_chargeMoney(card, money);

		// 增加一条用户资金变动记录
		UserRecord userRecord = new UserRecord();
		userRecord.setUserId(userId);
		userRecord.setType(2);
		userRecord.setMoney(money);
		userRecord.setCreateTime(new Date());

		int l = userRecordMapper.insert_userRecord(userRecord);
		if (i == 0 || j == 0 || k == 0 || l == 0) {
			throw new CustomException("佣金提现失败！");
		}
	}

	@Override
	public List<CommissionDetail> selectCommissionDetail(int shopId, int type) throws CustomException {
		List<CommissionDetail> commissionDetails = commissionDetailMapper.selectCommissionDetail(shopId, type);
		return commissionDetails;
	}

	@Override
	public Object selectCommissionGetMessage(int userId) throws CustomException {
		// 1、根据userId查询用户对应的商铺
		ShopWarp shopWarp = shopMapper.select_shopByUid(userId);
		if (shopWarp == null) {
			throw new CustomException("用户没有商铺，不能进行商铺佣金提现！");
		}
		// 3、查询商铺可用佣金
		double able_commission = shopMapper.select_able_commission(shopWarp.getId());
		// 4、根据商铺默认提现账号的到提现银行卡信息
		Card card = cardMapper.selectCard(shopWarp.getDefaultCardCode());
		// 5、将查询到的信息保存到Map集合中
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("shopWarp", shopWarp);
		map.put("able_commission", able_commission);
		map.put("card", card);
		return map;
	}

}
